<?php
namespace Home\Controller;
use Home\Model\EcmsNewsModel;
use Home\Model\EnewsadModel;
use Home\Model\EnewsclassModel;
use Home\Service\EcmsNewsService;
use Think\Controller;
class IndexController extends Controller {
    //主页
    public function index()
    {
        $newModel = new EcmsNewsModel();
        $arr = $newModel -> limit(2) -> select();

        //get adds 
        $addsModel = new EnewsadModel();
        $fields = array('picurl', 'url');
        $map['title'] = array('like','首页banner%');
        $addsData = $addsModel->getAdds($fields, $map);

        //获取所需要的文章
        $newModel = new EcmsNewsModel();
        $fields = 'id,titleurl,title,titlepic,smalltext';
        $order = "lastdotime desc";
        $newsArr = $newModel -> getNewsOrderByTime($fields, C('PAGE_SIZE'), $order);

        $this->assign('newsArr',$newsArr);
        $this->assign('newData',$addsData);
        $this->display();
    }

    /**
     * 文章详情页面
     */
    public function view()
    {
        $newId = I('get.newId');
        $newModel = new EcmsNewsModel();
        $newData = $newModel -> getNewById($newId); //文章详情信息

        //获取推荐阅读的数据
        $tuiJian = $newModel -> getTuiJianNews($newData['classid']);

        $this->assign('tuiData',$tuiJian);
        $this->assign('newData',$newData);
        $this->display();
    }

    /**
     * 新闻列表页面
     * list detail
     */
    public function listDetail()
    {
        $cat = I('get.cat'); //分类id
        if(!$cat)
        {
            $this->error('访问的分类不存在');
        }

        //判断是否有子级分类,如果有返回子类的数组，如果没有直接返回
        $classService = new EcmsNewsService();
        $catStr = $classService -> getChildClassIdArr($cat);
        //获取面包屑数据
        $breadcrumb = $classService->getBreadData($cat);
        //获取所需要的文章
        $newModel = new EcmsNewsModel();
        $fields = 'id,titleurl,title,titlepic,smalltext';
        $order = "lastdotime desc";
        $newsArr = $newModel -> getNewsByClassIdStr($catStr,$fields,C('PAGE_SIZE'),$order);

        $this->assign('newsArr',$newsArr);
        $this->assign('cat',$cat);
        $this->assign('breadcrumb', $breadcrumb);
        $this->display();
    }

    /**
     * 下拉列表时，返回请求的数据,前台ajax请求调用
     */
    public function pushDownList()
    {
        $getData = I('get.');
        
        $newsService = new EcmsNewsService();
        $result = $newsService -> getListForClass($getData); //请求数据处理

        return $result;
    }

    /**
     * 首页下拉列表时，返回请求的数据,前台ajax请求调用
     */
    public function slideUp()
    {

        $getData = I('get.');
        $newsService = new EcmsNewsService();
        $result = $newsService -> getListOrderByTime($getData); //请求数据处理

        return $result;
    }


}
